package qc.module.platform.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.utils.DateUtil;
import qc.module.platform.dto.blacklist.BlackListDto;
import qc.module.platform.entity.QcBlackList;

import java.util.Date;

/**
 * IpBlackListMapper
 *
 * @author QuCheng Tech
 * @since 2025/1/10
 */
@Mapper
public interface BlackListMapper {
    BlackListMapper MAPPER = Mappers.getMapper(BlackListMapper.class);

    @Mapping(target = "tm", source = "tm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "expiredtm", source = "expiredtm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    //@Mapping(target = "expiredtm", ignore = true)
    @Mapping(target = "isvalid", ignore = true)//是否过期需要根据是否有过期时间并与当前时间进行判断，在映射方法中单独处理
    @Mapping(target = "hittm", source = "hittm", dateFormat = "yyyy-MM-dd HH:mm:ss")
    BlackListDto toDto(QcBlackList en);

    default BlackListDto entityToDto(QcBlackList en) {
        BlackListDto dto = toDto(en);

        //默认当前是否有效为false
        dto.setIsvalid(false);

        Date now = DateUtil.getNowDate();
        if (en.getTm().before(now)) {
            dto.setIsvalid(true);
        }
        if (en.getExpiredtm() != null) {
            if (en.getExpiredtm().before(now))
                dto.setIsvalid(false);
        }

        return dto;
    }
}
